<style lang="scss" scoped> 
    .search_page{
        height: calc(100vh - 50px);
        overflow-y: auto;
        padding-top: 50px;
        background-color: #f5f5f5;
        .navigator{
            width: 100%;
            box-sizing: border-box;
            height: 50px;
            padding: 5px 4%;
            position: fixed;
            top: 0;
            left: 0;
            z-index: 999;
            display: flex;
            background-color: rgb(50, 134, 255);
            .headImg{
                width: 50px;
                position: relative;
                &::after{
                    position: absolute;
                    content: '';
                    width: 13px;
                    height: 13px;
                    border-top: 1px solid #fff;
                    border-left: 1px solid #fff;
                    transform: rotate(-45deg);
                    left: 0;
                    top: 15px;
                }
            }
            .search{
                width: calc(100% - 130px);
                line-height: 40px;
                height: 40px;
                input{
                    border: 1px solid #eee;
                    border-radius: 20px;
                    outline: none;
                    width: 100%;
                    padding-left: 10px;
                    background-color: rgba(255,255,255,0.1);
                    &::-webkit-input-placeholder{
                        color: #eee;
                    }
                }
            }
            .btn{
                width: 80px;
                text-align: right;
                button{
                    line-height: 40px;
                    background-color: rgba(0,0,0,0);
                    border: 0;
                    color: #fff;
                    &:active{
                        color: #333;
                    }
                }
            }
        }
        .search_history{
            width: 92%;
            padding: 10px 4%;
            min-height: 30px;
            background-color: #fff;
            .title{
                height: 30px;
                line-height: 30px;
                display: flex;
                justify-content: space-between;
            }
            .list{
                margin-top: 15px;
                span{
                    display: inline-block;
                    padding: 0 11px;
                    border-radius: 5px;
                    height: 30px;
                    line-height: 30px;
                    margin-right: 10px;
                    margin-bottom: 25px;
                    background-color: #999;
                    color: #eee;
                    font-size: 14px;
                }
            }
        }
        .product{
            width: 90%;
            margin-left: 5%;
            margin-top: 15px;
            min-height: 105vh;
            display: flex;
            justify-content: space-between;
            .proLeft,
            .proRight{
                width: 48%;
                .proList{
                    background-color: #fff;
                    border-radius: 7px;
                    overflow: hidden;
                    margin-bottom: 20px;
                    img{
                        width: 100%;
                    }
                    .proName{
                        height: 45px;
                        box-sizing: border-box;
                        padding: 5px 7px;
                        line-height: 20px;
                        // 省略号
                        overflow: hidden;
                        text-overflow: ellipsis;
                        display: -webkit-box;
                        -webkit-line-clamp: 2;
                        -webkit-box-orient: vertical;
                    }
                    .pri{
                        height: 35px;
                        box-sizing: border-box;
                        padding: 3px 7px;
                        line-height: 29px;
                        display: flex;
                        justify-content: space-between;
                        .nP{
                            color: #ff0000;
                        }
                        span{
                            font-size: 12px;
                            font-weight: bold;
                        }
                        .oP{
                            font-size: 14px;
                            text-decoration: line-through;
                            color: #999;
                        }
                    }
                    .number{
                        padding: 0 7px;
                        height: 30%;
                        line-height: 35px;
                        display: flex;
                        justify-content: space-between;
                        font-size: 14px;
                        color: #999;
                    }
                }
            }
        }
    }
</style>
<template>
    <div class="search_page">
        <div class="navigator">
            <div class="headImg" @click="$router.back()">
                <!-- <img :src="headImg === null ? '' : headImg" alt=""> -->
            </div>
            <div class="search">
                <input
                    @focus="isFocus(true)"
                    @blur="isFocus(false)"
                    v-model="objPage.keyword"
                    type="text"
                    placeholder="搜索">
            </div>
            <div class="btn">
                <button @click="product_list=[],objPage.page=0,loading=true,focus=false,getList()">
                    搜索
                </button>
            </div>
        </div>
        <div class="search_history" v-if="focus === true">
            <div class="title">
                <span>
                    搜索历史
                </span>
                <span>
                    ✖
                </span>
            </div>
            <div class="list">
                <span>
                    上衣
                </span>
            </div>
        </div>
        <div class="product" v-else>
            <div class="proLeft">
                <div v-for="(item,index) in product_list" :key="item.ID">
                    <div class="proList" 
                        v-if="index % 2 != 0"
                        @click="$router.push(`/product_detail?product_id=${item.ID}&admin_id=${item.admin_id}`)"
                        >
                        <div class="proImg">
                            <img :src="item.duck_img" alt="">
                        </div>
                        <div class="proName">
                            {{ item.duck_name }}
                        </div>
                        <div class="pri">
                            <div class="nP">
                                <span>
                                    ￥
                                </span>
                                {{ item.duck_min_price }}
                            </div>
                            <div class="oP">
                                ￥{{ item.duck_no_price }}
                            </div>
                        </div>
                        <div class="number">
                            <span>
                                销量:{{ 
                                        item.duck_hot > 999 ? '999+': item.duck_hot
                                    }}
                            </span>
                            <span>
                                库存: {{ 
                                        item.num > 999 ? '999+' : item.num
                                    }}
                            </span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="proRight">
                <div v-for="(item,index) in product_list" :key="item.ID">
                    <div class="proList" 
                        v-if="index % 2 == 0"
                        @click="$router.push(`/product_detail?product_id=${item.ID}&admin_id=${item.admin_id}`)"
                        >
                        <div class="proImg">
                            <img :src="item.duck_img" alt="">
                        </div>
                        <div class="proName">
                            {{ item.duck_name }}
                        </div>
                        <div class="pri">
                            <div class="nP">
                                <span>
                                    ￥
                                </span>
                                {{ item.duck_min_price }}
                            </div>
                            <div class="oP">
                                ￥{{ item.duck_no_price }}
                            </div>
                        </div>
                        <div class="number">
                            <span>
                                销量:{{ 
                                        item.duck_hot > 999 ? '999+': item.duck_hot
                                    }}
                            </span>
                            <span>
                                库存: {{ 
                                        item.num > 999 ? '999+' : item.num
                                    }}
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
// 导出获取商品列表的接口
import {
    getProductList
} from '@/api'
    export default {
        methods: {
            // 判断输入框是否获取焦点的
            isFocus(bool){
                this.focus = bool;
            },
            getList(){
                if(!this.loading){
                    this.$toast('没有更多商品了~')
                    return
                }
                getProductList(this.objPage).then(res => {
                    // 如果返回的数据和请求的数据长度不一致，证明下一页也没有数据，就不再请求更多数据
                    if(res.data.result.length !== this.objPage.limit){
                        this.loading = false;
                    }
                    res.data.result.forEach(item => {
                        item.duck_img = item.duck_img.replace(/www.poralmall.com/g,'192.168.13.10')
                        .replace(/192.168.17.126/g,'192.168.13.10')
                        // 192.168.17.126
                    })
                    if(res.data.code == 1){
                        this.product_list = [
                            ...this.product_list,
                            ...res.data.result
                        ]
                    }
                })
            },
            elements(event){
                // 获取到当前发生事件的对象
                let target = event.target || event.srcElement;
                // 可以滚动的高度
                // target.scrollHeight
                // 已经滚动的高度
                // target.scrollTop
                // 视图的高度
                // target.offsetHeight
                if(target.scrollHeight - target.scrollTop <= target.offsetHeight+2){
                    // console.log(123);
                    // 触底事件发生
                    // 先让页面+1
                    this.objPage.page ++;
                    // 再去获取数据
                    this.getList();
                }
            }
        },
        data() {
            return {
                product_list: [],
                focus: true,
                objPage: {
                    type: '0',
                    keyword: '',
                    page: 0,
                    limit: 4,
                    admin_id: '125'
                },
                // 是否可以加载
                loading: true
            }
        },
        mounted(){
            document.querySelector('.search_page').addEventListener('scroll',this.elements)
        }
    }
</script>